﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test1
{
    public partial class Form1 : Form
    {
        #region MyRegion
        ////private Point Position = new Point(0, 0);
        ////public Form1()
        ////{
        ////    InitializeComponent();
        ////}

        ////private void button1_Click(object sender, EventArgs e)
        ////{
        ////    Button btn = new Button();
        ////    this.SuspendLayout();
        ////    btn.Location = new System.Drawing.Point(100, 150);
        ////    btn.Name = "btn";
        ////    btn.Size = new System.Drawing.Size(75, 23);
        ////    btn.TabIndex = 0;
        ////    btn.Text = "btn";
        ////    btn.UseVisualStyleBackColor = true;
        ////    btn.Show();
        ////    MessageBox.Show("fff");
        ////}

        ////private void Form1_Load(object sender, EventArgs e)
        ////{
        ////    this.treeView1.ExpandAll();
        ////}

        ////private void treeView1_DragDrop(object sender, DragEventArgs e)
        ////{
        ////    TreeNode myNode = null;
        ////    if (e.Data.GetDataPresent(typeof(TreeNode)))
        ////    {
        ////        myNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));
        ////    }
        ////    else
        ////    {
        ////        MessageBox.Show("error");
        ////    }
        ////    Position.X = e.X;
        ////    Position.Y = e.Y;
        ////    Position = treeView1.PointToClient(Position);
        ////    TreeNode DropNode = this.treeView1.GetNodeAt(Position);
        ////    // 1.目标节点不是空。2.目标节点不是被拖拽接点的字节点。3.目标节点不是被拖拽节点本身
        ////    if (DropNode != null && DropNode.Parent != myNode && DropNode != myNode)
        ////    {
        ////        TreeNode DragNode = myNode;
        ////        // 将被拖拽节点从原来位置删除。
        ////        myNode.Remove();
        ////        // 在目标节点下增加被拖拽节点
        ////        DropNode.Nodes.Add(DragNode);
        ////    }
        ////    // 如果目标节点不存在，即拖拽的位置不存在节点，那么就将被拖拽节点放在根节点之下
        ////    if (DropNode == null)
        ////    {
        ////        TreeNode DragNode = myNode;
        ////        myNode.Remove();
        ////        treeView1.Nodes.Add(DragNode);
        ////    }
        ////}

        ////private void treeView1_DragEnter(object sender, DragEventArgs e)
        ////{
        ////    if (e.Data.GetDataPresent(typeof(TreeNode)))
        ////        e.Effect = DragDropEffects.Move;
        ////    else
        ////        e.Effect = DragDropEffects.None;
        ////}

        ////private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
        ////{
        ////    DoDragDrop(e.Item, DragDropEffects.Move);
        ////} 
        #endregion

        #region 2
        ////private int count = 1;
        ////private void Form1_Load(System.Object sender, System.EventArgs e)
        ////{
        ////    this.AllowDrop = true;
        ////    //窗体自身支持接受拖拽来的控件
        ////}

        ////private void Button1_MouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e)
        ////{
        ////    //左键的话，标志位为true（表示拖拽开始）
        ////    if ((e.Button == System.Windows.Forms.MouseButtons.Left))
        ////    {
        ////        Button1.DoDragDrop(Button1, DragDropEffects.Copy | DragDropEffects.Move);
        ////        //形成拖拽效果，移动+拷贝的组合效果
        ////    }
        ////}

        ////private void Form1_DragEnter(System.Object sender, System.Windows.Forms.DragEventArgs e)
        ////{
        ////    //当Button被拖拽到WinForm上时候，鼠标效果出现
        ////    if ((e.Data.GetDataPresent(typeof(Button))))
        ////    {
        ////        e.Effect = DragDropEffects.Copy;
        ////    }
        ////}

        ////private void Form1_DragDrop(System.Object sender, System.Windows.Forms.DragEventArgs e)
        ////{
        ////    //拖放完毕之后，自动生成新控件
        ////    Button btn = new Button();
        ////    btn.Size = Button1.Size;
        ////    btn.Location = this.PointToClient(new Point(e.X, e.Y));
        ////    //用这个方法计算出客户端容器界面的X，Y坐标。否则直接使用X，Y是屏幕坐标
        ////    this.Controls.Add(btn);
        ////    btn.Text = "按钮" + count.ToString();
        ////    count = count + 1;
        ////}
        ////public Form1()
        ////{
        ////    DragDrop += Form1_DragDrop;
        ////    DragEnter += Form1_DragEnter;
        ////    Load += Form1_Load;
        ////} 
        #endregion

    }
}
